<template>
  <view>
    <view class="goods-list">
     <block v-for="(item, i) in goodsList" :key="i">
        <!-- 为 my-goods 组件动态绑定 goods 属性的值 -->
        <my-goods  :goods="item"></my-goods>
      </block>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        // 请求参数对象
        queryObj:{
          query:'',
          cid:'',
          pagenum:1,
          pagesize:10
        },
        goodsList:[],
        total:0,
        // 节流阀
        isLoading:false
      };
    },
    onLoad(options){
      this.queryObj.query=options.query||''
      this.queryObj.cid=options.cid||''
      // console.log(this.queryObj);
      this.getGoodsList()
    },
      
    methods:{
    async getGoodsList(cb){
      this.isLoading=true
          const{data:res}=  await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
          if(res.meta.status!==200)return uni.$showMsg()
          // this.goodsList=res.message.goods
          this.goodsList=[...this.goodsList,...res.message.goods]
          this.total=res.message.total
          this.isLoading=false
          // 短路运算符
          cb && cb()
      }
    },
    // 上拉加载更多
    onReachBottom() {
      // 判断是否数据加载完毕
      if(this.queryObj.pagenum*this.queryObj.pagesize>=this.total)return uni.$showMsg('数据加载完毕')
      // 判断是否正在发送网络请求
      if(this.isLoading===true)return
      this.queryObj.pagenum++
      this.getGoodsList()
    },
    onPullDownRefresh() {
      this.queryObj.pagenum=1
      this.total=0
      this.isLoading=false
      this.getGoodsList(()=>{uni.stopPullDownRefresh()})
    },
   
  }
</script>

<style lang="scss">
.goods-list{
 
  
}
</style>
